﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GoPlay.Models;

namespace GoPlay.Controllers
{
    [Authorize]
    public class GamesController : Controller
    {
        IRepository<Game> _repository;

        public GamesController() : this(new EFRepository<Game>()) { }

        public GamesController(IRepository<Game> repository)
        {
            _repository = repository;
        }


        //
        // GET: /Games/Create

        public ActionResult Create()
        {
            var Game = new Game();
            return View(Game);
        }

        //
        // POST: /Games/Create

        [HttpPost]
        public ActionResult Create(Game game)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Insert(game);
                    _repository.SaveChanges();
                    Session["FlashSuccess"] = "Your Game was created.";
                    //return RedirectToAction("Details", "Teams", new { id = RouteData.Values["TeamId"].ToString() }); //("Index");
                    return RedirectToAction("Details", "Teams", new { id = game.TeamId }); //("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View(game);
        }

        //
        // GET: /Games/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Game c = _repository.GetByID(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            return View(c);
        }

        //
        // POST: /Games/Edit/5

        [HttpPost]
        public ActionResult Edit(Game Game)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Update(Game);
                    _repository.SaveChanges();
                    Session["FlashSuccess"] = "Your Game was updated.";
                    return RedirectToAction("Details", "Teams", new { id = RouteData.Values["TeamId"].ToString() });
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View(Game);
        }

        //
        // GET: /Games/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Game c = _repository.GetByID(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            return View(c);
        }

        //
        // POST: /Games/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {

            try
            {
                Game Game = _repository.GetByID(id);
                _repository.Delete(Game);
                _repository.SaveChanges();
                Session["FlashSuccess"] = "Your game was deleted.";
                return RedirectToAction("Details", "Teams", new { id = RouteData.Values["TeamId"].ToString() }); //("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View();
        }
    }
}